<template>
  <el-container class="layout">
    <el-header>
      <div class="header-content">
        <div class="logo">医键三连</div>
        <el-menu mode="horizontal" :default-active="activeMenu">
          <el-menu-item index="/" @click="navigateTo('/')">首页</el-menu-item>
          <el-menu-item index="/smart-guide" @click="navigateTo('/smart-guide')">智慧导诊</el-menu-item>
          <el-menu-item index="/appointments" @click="navigateTo('/appointments')">陪诊预约</el-menu-item>
          <el-menu-item index="/ai-chat" @click="navigateTo('/ai-chat')">智慧问诊</el-menu-item>
          <el-menu-item index="/about-us" @click="navigateTo('/about-us')">关于我们</el-menu-item>
        </el-menu>
        <div class="user-info">
          <el-dropdown @command="handleCommand">
            <span class="user-name">
              {{ userName }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    
    <el-main>
      <div class="main-content">
        <Suspense>
          <template #default>
            <router-view></router-view>
          </template>
          <template #fallback>
            <div class="loading">
              <el-skeleton :rows="10" animated />
            </div>
          </template>
        </Suspense>
      </div>
    </el-main>
    
    <el-footer>
      <div class="footer-content">
        <p>© 2024 医疗陪诊系统 版权所有</p>
      </div>
    </el-footer>
  </el-container>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()

const userName = computed(() => localStorage.getItem('username') || '用户名')
const activeMenu = computed(() => route.path)

const navigateTo = async (path) => {
  console.log('Navigating to:', path)
  try {
    await router.push(path)
  } catch (error) {
    if (error.name !== 'NavigationDuplicated') {
      console.error('Navigation error:', error)
      ElMessage.error('页面跳转失败，请重试')
    }
  }
}

const handleCommand = (command) => {
  switch (command) {
    case 'logout':
      localStorage.removeItem('token')
      localStorage.removeItem('username')
      router.push('/login')
      break
    case 'profile':
    case 'settings':
      ElMessage.info('功能开发中...')
      break
  }
}

onMounted(() => {
  console.log('Layout mounted, current route:', route.path)
})
</script>

<style scoped lang="scss">
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  position: fixed;
  width: 100%;
  z-index: 100;
  top: 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;

  .logo {
    font-size: 20px;
    font-weight: bold;
    color: #409EFF;
  }

  .user-info {
    .user-name {
      cursor: pointer;
      color: #606266;
      display: flex;
      align-items: center;
      gap: 4px;
    }
  }
}

.el-main {
  margin-top: 60px;
  flex: 1;
  padding: 20px;
  background: #f5f7fa;
}

.el-footer {
  background: #fff;
  padding: 0;
  
  .footer-content {
    max-width: 1200px;
    margin: 0 auto;
    text-align: center;
    padding: 20px;
    color: #909399;
  }
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  min-height: calc(100vh - 180px);
}

.loading {
  padding: 20px;
}
</style> 